//SVGExsample.cpp
//
#include <windows.h>
#include "stdafx.h"
#define EXPORTING_DLL
#include "SVGExsample.h"
#include "SVGDrawing.h"

using namespace std;

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}

LPTSTR readXMLData(void *handler, LPCTSTR value, CHandler *PHandler)
{
	LPTSTR temp;
	temp = (LPTSTR)malloc((250 + 1)*sizeof(TCHAR));
	PHandler->getValue(value, temp, 250, handler);
	return temp;
}

/*
LPTSTR readXMLData(LPCTSTR fpath, LPCTSTR value)
{
	LPTSTR temp;
	temp = (LPTSTR)malloc((250 + 1)*sizeof(TCHAR));
	_bstr_t path(_T(""));
	CHandler *PHandler;
	PHandler = new CHandler(path);

	// read data file with house information
	//void * handler = PHandler->initHandlerFromFile(_T("C:\\Users\\Ming Sheng\\Desktop\\house.xml"), NULL);
	void * handler = PHandler->initHandlerFromFile(fpath, NULL);
	// read house values
	PHandler->getValue(value, temp, 250, handler);
	delete PHandler;
	return temp;
}
*/

/*
 *draw a house:roof, door and window
 *The x defines the left position of the house
 *The y defines the top position of the house
 *The width and height define the height and the width of the house
*/
void draw(LPCTSTR fpath)
{
	LPTSTR temp;
	_bstr_t path(_T(""));
	CHandler *PHandler;
	PHandler = new CHandler(path);

	// read data file with house information
	void * handler = PHandler->initHandlerFromFile(fpath, NULL);
	temp = readXMLData(handler, _T("/house/width"), PHandler);
	//temp = readXMLData(fpath, _T("/house/width"));
	int iwidth = _wtoi(temp);
	temp = readXMLData(handler, _T("/house/height"), PHandler);
	int iheight = _wtoi(temp);
	temp = readXMLData(handler, _T("/house/x"), PHandler);
	int ix = _wtoi(temp);
	temp = readXMLData(handler, _T("/house/y"), PHandler);
	int iy = _wtoi(temp);

	delete PHandler;

	if(ix<0 || iy<0 || iwidth<=0 || iheight<=0)
	{
		cerr<<"Error in draw function: x, y, width, height must greater than null"<<endl;
		exit(1);

	}

	SVGDrawing test;	
	//create roof
	//hRoof defines the height of the roof
	//x,y defines the position of the roof
	double hRoof = iheight*0.25;	
	double xRoof = ix+iwidth/2;
	double yRoof = iy+hRoof;	
	test.createLine(ix, yRoof, xRoof, iy);
	test.createLine(xRoof , iy, ix+iwidth, yRoof);
	//create room
	double hRoom = iheight-hRoof;
	test.createRect(ix, yRoof, iwidth, hRoom);
	//create door
	//xDoor, yDoor define the position of the door
	//wDoor defines the width of the door and hDoor defines the height of the door 
	double xDoor = ix + iwidth*0.375;
	double yDoor = yRoof + iheight*0.5;
	double wDoor = iwidth*0.2;
	double hDoor = iheight*0.25;
	test.createRect(xDoor, yDoor, wDoor, hDoor);
	//create window
	//xWindow, yWindow define the position of the window
	//wWindow defines the width of the window and hWindow defines the height of the window 
	double xWindow = ix + iwidth*0.35;
	double yWindow = yRoof + iheight*0.15;
	double wWindow = iwidth*0.3;
	double hWindow = iheight*0.15;
	test.createRect(xWindow, yWindow, wWindow, hWindow);
	test.saveToFile(".//house.svg");
}
/*
void drawRoof(const SVGDrawing &rt, const int x, const int y, const int width, const int height)
{
	int y1 = y-width/2;
	if (y1<0)
	{
		cerr<<"Error: y-axis must greater than null";
		exit(1);
	}
	int x1 = x+width/2;
	rt.createLine(x, y, x1, y1);
	rt.createLine(x1 , y1, x+width, y);
	rt.createRect(x, y, width, height-y1);
}
*/
/*
void drawRoom(const SVGDrawing &rt, const int x, const int y, const int width, const int height)
{
	rt.createRect(x, y, width, height-width/2);
}
*/
